from flask import Flask, jsonify, request
# 导入配制文件
from config import Config
from db import db1
from flask_cors import CORS

import json

app = Flask(__name__)
# 加载配制文件
app.config.from_object(Config)
# 跨域配制
CORS(app)


def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data


# 添加展示工作流接口
@app.route('/adds', methods=['POST', 'GET'])
def add():
    if request.method == "POST":
        data = get_data()
        sql = "insert into worktemp(name) values('%s')" % (data['name'])
        db1.update(sql)
        return jsonify({'code': 200})
    else:
        sql1 = "select * from worktemp"
        res = db1.find_all(sql1)
        return jsonify({'code': 200, 'list': res})


# 设置工作流属性
@app.route('/setparams', methods=['POST', 'GET'])
def setparams():
    if request.method == "POST":
        data = get_data()
        # 更新工作流中的参数
        sql = "update worktemp set params='%s' where id=%d" % (data['params'], int(data['id']))
        db1.update(sql)
        return jsonify({"code": 200})
    else:
        id = request.args.get('wid')
        sql = "select * from worktemp where id=%d" % (int(id))
        res = db1.find(sql)
        return jsonify({"code": 200, 'workf': res})


if __name__ == "__main__":
    app.run()
